import 'package:aero_frisbee/app/model/local_post_model/local_post_model.dart';
import 'package:aero_frisbee/app_route.dart';
import 'package:aero_frisbee/gen/assets.gen.dart';
import 'package:aero_frisbee/pages/modues/home/view_model/af_home_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

class AfHomeCell extends ConsumerWidget {
  const AfHomeCell({Key? key, required this.item}) : super(key: key);

  final LocalPostModel item;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        // ref
        //     .read(appApiProvider)
        //     .fetchUserInfo(userId: '${item.cacheBind?.homeUser?.userId}');
        context.pushNamed(RouteUri.postDetail, extra: item);
      },
      child: Stack(
        children: [
          Container(
            decoration: const BoxDecoration(
              color: Colors.white,
            ),
            padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12),
                boxShadow: [
                  BoxShadow(
                    color: const Color(0xff81BA07).withAlpha(8),
                    spreadRadius: 0,
                    blurRadius: 9,
                    offset: const Offset(0, 3), // 阴影偏移
                  ),
                ],
                border: Border.all(
                  color: const Color(0xffEEEEEE),
                  width: 0.5,
                ),
              ),
              padding: const EdgeInsets.only(right: 8),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Stack(
                    children: [
                      SizedBox(
                        width: 103,
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(12),
                          child: AspectRatio(
                            aspectRatio: 306 / 324,
                            child: Image.asset(
                              'assets/res/${item.postCover ?? ''}',
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                      ),
                      Positioned.fill(
                        child: Assets.images.home.homeCellImgBg
                            .image(width: 103, height: 108),
                      )
                    ],
                  ),
                  const SizedBox(
                    width: 10,
                  ),
                  Expanded(
                    child: Column(
                      children: [
                        const SizedBox(
                          height: 10,
                        ),
                        Align(
                          alignment: Alignment.topLeft,
                          child: Text(
                            item.title ?? '',
                            style: const TextStyle(
                              color: Color(0xff202020),
                              fontWeight: FontWeight.normal,
                              fontSize: 12,
                            ),
                            textAlign: TextAlign.left,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                          ),
                        ),
                        const SizedBox(
                          height: 10,
                        ),
                        Align(
                          alignment: Alignment.topLeft,
                          child: Text(
                            item.desc ?? '',
                            style: const TextStyle(
                              color: Color(0xff565656),
                              fontWeight: FontWeight.normal,
                              fontSize: 10,
                            ),
                            textAlign: TextAlign.left,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                          ),
                        ),
                        const SizedBox(
                          height: 6,
                        ),
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Flexible(
                              child: Container(
                                decoration: BoxDecoration(
                                  color: const Color(0xffFFFACD),
                                  borderRadius: BorderRadius.circular(12),
                                ),
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 2, vertical: 2),
                                child: Row(
                                  mainAxisSize: MainAxisSize.min,
                                  children: [
                                    CircleAvatar(
                                      radius: 10,
                                      backgroundImage: NetworkImage(
                                        item.cacheBind?.homeUser?.avatar ?? '',
                                      ),
                                    ),
                                    Flexible(
                                      child: Padding(
                                        padding: const EdgeInsets.only(
                                            top: 4,
                                            bottom: 6,
                                            right: 8,
                                            left: 8),
                                        child: Text(
                                          item.cacheBind?.homeUser?.nickname ??
                                              '',
                                          style: const TextStyle(
                                            color: Color(0xff303507),
                                            fontWeight: FontWeight.normal,
                                            fontSize: 10,
                                          ),
                                          textAlign: TextAlign.left,
                                          overflow: TextOverflow.ellipsis,
                                          maxLines: 1,
                                        ),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ),
                            GestureDetector(
                              behavior: HitTestBehavior.opaque,
                              onTap: () {
                                ref
                                    .read(
                                        afHomePostListServiceProvider.notifier)
                                    .toggleLike(item.uid ?? 0);
                              },
                              child: Padding(
                                padding: const EdgeInsets.only(right: 8),
                                child: item.isLike
                                    ? Assets.icons.iconLikeLight
                                        .image(width: 15)
                                    : Assets.icons.iconLikeGray
                                        .image(width: 15),
                              ),
                            )
                          ],
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
          Positioned(
            top: 2,
            right: 12,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
              ),
              child: item.markImg,
            ),
          )
        ],
      ),
    );
  }
}
